/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NovoCampeonato.java
 *
 * Created on 04/06/2011, 11:08:21
 */
package campeonatobrasuka;

import Persistencia.daos.CampeonatoDAO;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Jaqueline Ishiyama
 */
public class NovoCampeonato extends javax.swing.JFrame {

    DefaultFormatter form = new DefaultFormatter();
    Persistencia.modelos.Campeonato novoCampeonato = new Persistencia.modelos.Campeonato();
    CampeonatoDAO campeonato = new CampeonatoDAO();

    /** Creates new form NovoCampeonato */
    public NovoCampeonato() {
        initComponents();
        this.jButton5.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jSeparator1 = new javax.swing.JSeparator();
        jButton5 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        dtInicio = new javax.swing.JFormattedTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        dtFim = new javax.swing.JFormattedTextField();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(campeonatobrasuka.CampeonatoBrasukaApp.class).getContext().getResourceMap(NovoCampeonato.class);
        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setEnabled(false);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setEnabled(false);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        dtInicio.setFormatterFactory(form.setFormatoData());
        dtInicio.setName("dtInicio"); // NOI18N

        jTextField3.setName("jTextField3");
        jTextField3.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField3FocusGained(evt);
            }
        });
        jTextField3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField3KeyPressed(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        dtFim.setFormatterFactory(form.setFormatoData());
        dtFim.setName("dtFim"); // NOI18N

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(campeonatobrasuka.CampeonatoBrasukaApp.class).getContext().getActionMap(NovoCampeonato.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(39, 39, 39)
                        .addComponent(dtInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(63, 63, 63)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dtFim, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(91, 91, 91)
                        .addComponent(jLabel3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(417, 417, 417))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 1032, Short.MAX_VALUE)
                .addGap(27, 27, 27))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(713, 713, 713)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1049, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(dtInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(dtFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton1)
                    .addComponent(jButton5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 495, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // método de ação para o botão confirmar

        String dataInicio = this.dtInicio.getText();// campo data de início do campeonato
        String datFim = this.dtFim.getText();//campo data fim do campeonmato
        String partidas = this.jTextField3.getText();// campo qtd de partidas
        String ano = dataInicio.substring(6, 10);
        int novaPartida = 0;
        if (!(partidas.isEmpty())) {
            boolean ehInt = (form.ValidaNumero(jTextField3));
            if (ehInt == false) {
            } else {
                novaPartida = Integer.parseInt(partidas);
            }

        }

        if ((dataInicio.isEmpty()) || (datFim.isEmpty()) || (partidas.isEmpty())) {
            //exibe popup de erro
            JOptionPane.showMessageDialog(null, "Todos os campos são obrigatórios!", "Alerta", JOptionPane.WARNING_MESSAGE);

        } else if ((novaPartida > 380) || (novaPartida < 1)) {
            JOptionPane.showMessageDialog(null, "O campo quantidade de partidas só aceita números entre 1 e 380!", "Alerta", JOptionPane.WARNING_MESSAGE);


        } else if ((form.validadorData(dataInicio)) && (form.validadorData(datFim))) {
            Date fim = null;
            try {
                fim = form.formataData(datFim);
            } catch (Exception ex) {
                Logger.getLogger(NovoCampeonato.class.getName()).log(Level.SEVERE, null, ex);
            }
            Date inicio = null;
            try {
                inicio = form.formataData(dataInicio);
            } catch (Exception ex) {
                Logger.getLogger(NovoCampeonato.class.getName()).log(Level.SEVERE, null, ex);
            }
            novoCampeonato.setDataFim(fim);
            novoCampeonato.setDataInicio(inicio);
            novoCampeonato.setNomeCampeonato("Campeonato_Brasileiro_".concat(ano));
            novoCampeonato.setQtdePartidas(novaPartida);
            campeonato.atualiza(novoCampeonato);

            this.dtInicio.setEnabled(false);// campo data de início do campeonato
            this.dtFim.setEnabled(false);//campo data fim do campeonmato
            this.jTextField3.setEnabled(false);// campo qtd de partidas
            this.jButton5.setVisible(false);//botão confirmar
            this.jButton3.setVisible(true);//botão alterar campeonato (informações)
            this.jButton4.setVisible(true); //botão incluir
        } else//se data não for válida exibe um popup
        {
            JOptionPane.showMessageDialog(null, " Verifique se os campos de datas estão válidos!", "Alerta", JOptionPane.WARNING_MESSAGE);



        }

    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // MÉTODO DE AÇÃO DO BOTÃO ALTERAR
        this.dtInicio.setEnabled(true);// campo data de início do campeonato
        this.dtFim.setEnabled(true);//campo data fim do campeonmato
        this.jTextField3.setEnabled(true);// campo qtd de partidas
        this.jButton3.setVisible(false);//botão alterar campeonato (informações)
        this.jButton4.setVisible(false);
        this.jButton5.setVisible(true);


}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        //  Evento do Botão Incluir novos Componentes

        InserirComponentes novo = new InserirComponentes();

        jScrollPane1.setViewportView(novo);
        jButton1.setEnabled(false);
        jButton3.setEnabled(false);
 
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked

        // TODO add your handling code here:

        this.dtInicio.setEnabled(false);// campo data de início do campeonato
        this.dtFim.setEnabled(false);//campo data fim do campeonmato
        this.jTextField3.setEnabled(false);// campo qtd de partidas
        this.jButton4.setEnabled(true);// botão incluir componentes
        this.jButton3.setEnabled(true);//botão alterar campeonato (informações)
        this.jButton1.setEnabled(false);//botão OK

        String dataInicio = this.dtInicio.getText();// campo data de início do campeonato
        String datFim = this.dtFim.getText();//campo data fim do campeonmato
        String partidas = this.jTextField3.getText();// campo qtd de partidas
        String ano = dataInicio.substring(6, 10);
        int novaPartida = 0;
        if (!(partidas.isEmpty())) {
            boolean ehInt = (form.ValidaNumero(jTextField3));
            if (ehInt == false) {

                this.dtInicio.setEnabled(true);// campo data de início do campeonato
                this.dtFim.setEnabled(true);//campo data fim do campeonmato
                this.jTextField3.setEnabled(true);// campo qtd de partidas
                this.jButton4.setEnabled(false);// botão incluir componentes
                this.jButton3.setEnabled(false);//botão alterar campeonato (informações)
                this.jButton1.setEnabled(true);//botão OK

            } else {
                novaPartida = Integer.parseInt(partidas);
            }
        }

        if ((dataInicio.isEmpty()) || (datFim.isEmpty()) || (partidas.isEmpty())) {
            //exibe popup de erro
            JOptionPane.showMessageDialog(null, "Todos os campos são obrigatórios!", "Alerta", JOptionPane.WARNING_MESSAGE);

            this.dtInicio.setEnabled(true);// campo data de início do campeonato
            this.dtFim.setEnabled(true);//campo data fim do campeonmato
            this.jTextField3.setEnabled(true);// campo qtd de partidas
            this.jButton4.setEnabled(false);// botão incluir componentes
            this.jButton3.setEnabled(false);//botão alterar campeonato (informações)
            this.jButton1.setEnabled(true);//botão OK

        } else if ((novaPartida > 380) || (novaPartida < 1)) {
            JOptionPane.showMessageDialog(null, "O campo quantidade de partidas só aceita números entre 1 e 380!", "Alerta", JOptionPane.WARNING_MESSAGE);

            this.dtInicio.setEnabled(true);// campo data de início do campeonato
            this.dtFim.setEnabled(true);//campo data fim do campeonmato
            this.jTextField3.setEnabled(true);// campo qtd de partidas
            this.jButton4.setEnabled(false);// botão incluir componentes
            this.jButton3.setEnabled(false);//botão alterar campeonato (informações)
            this.jButton1.setEnabled(true);//botão OK
        } else if ((form.validadorData(dataInicio)) && (form.validadorData(datFim))) {
            Date fim = null;
            try {
                fim = form.formataData(datFim);
            } catch (Exception ex) {
                Logger.getLogger(NovoCampeonato.class.getName()).log(Level.SEVERE, null, ex);
            }
            Date inicio = null;
            try {
                inicio = form.formataData(dataInicio);
            } catch (Exception ex) {
                Logger.getLogger(NovoCampeonato.class.getName()).log(Level.SEVERE, null, ex);
            }
            novoCampeonato.setDataFim(fim);
            novoCampeonato.setDataInicio(inicio);
            novoCampeonato.setNomeCampeonato("Campeonato_".concat(ano));
            novoCampeonato.setQtdePartidas(novaPartida);
            campeonato.salva(novoCampeonato);
        } else//se data não for válida exibe um popup
        {
            JOptionPane.showMessageDialog(null, " Verifique se os campos de datas estão válidos!", "Alerta", JOptionPane.WARNING_MESSAGE);

            this.dtInicio.setEnabled(true);// campo data de início do campeonato
            this.dtFim.setEnabled(true);//campo data fim do campeonmato
            this.jTextField3.setEnabled(true);// campo qtd de partidas
            this.jButton4.setEnabled(false);// botão incluir componentes
            this.jButton3.setEnabled(false);//botão alterar campeonato (informações)
            this.jButton1.setEnabled(true);//botão OK

        }





    }//GEN-LAST:event_jButton1MouseClicked

    private void jTextField3KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField3KeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3KeyPressed

    private void jTextField3FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField3FocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3FocusGained

    public Persistencia.modelos.Campeonato retornaCampeonato(){
        return novoCampeonato;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NovoCampeonato().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField dtFim;
    private javax.swing.JFormattedTextField dtInicio;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JMenuBar menuBar;
    // End of variables declaration//GEN-END:variables
}
